package fundation.tree;

public class Node<T extends Comparable<?>> {
    Node<T> left, right;
    T data;

    public Node() {
    }

    public Node(T data) {
        this.data = data;
    }

    /**
     * 从完全二叉树中构造二叉树
     *
     * @param completeBinaryTree
     * @return
     */
    public static <T extends Comparable<?>> Node<T> createInstanceFromList(T[] completeBinaryTree) {
        Node<T> root = new Node();
        fillRoot(root, completeBinaryTree, 0);

        return root;
    }

    public static <T extends Comparable<?>> void fillRoot(Node<T> root, T[] arr, int index) {
        int length = arr.length;
        root.data = arr[index];
        int leftIdx = (index << 1) + 1;
        int rightIdx = leftIdx + 1;

        if (leftIdx < length) {
            root.left = new Node<>();
            fillRoot(root.left, arr, leftIdx);
        }
        if (rightIdx < length) {
            root.right = new Node<>();
            fillRoot(root.right, arr, rightIdx);
        }
    }

    public static void main(String[] args) {
        Node node = new Node();
        node.constructFromListTest();
    }

    public void constructFromListTest() {
        Integer[] ss = new Integer[]{1, 7, 3, 8, 9, 6};
//        String[] ss = new String[]();
        Node<Integer> root = createInstanceFromList(ss);
        BTreePrinter.printNode(root);

    }
}
